package com.puzzle;

import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

// 图片控件
@SuppressWarnings("serial")
class PictureCtrl extends JPanel implements MouseListener {
    // 回调
    public interface Callback {
        void movePicture(PictureCtrl pictureCtrl); // 图片控件的单击事件
    }

    private final Callback mCallback;       // 回调
    private boolean mHasBorder;             // 有边框则为 true，否则为 false
    private final BufferedImage mImage;     // 图片
    private final Point mOriginalPosition;  // 原位置，即刚被创建时的初始位置

    /**
     * 构造方法。
     * @param Callback  回调
     * @param hasBorder 有边框则为 true，否则为 false
     * @param image     图片
     * @param parent    父窗口
     * @param rect      位置、尺寸
     */
    public PictureCtrl(Callback Callback, boolean hasBorder, Image image, Container parent, Rectangle rect) {
        super(true);

        // 初始化成员
        mCallback = Callback;
        mHasBorder = hasBorder;
        super.setBounds(rect);
        mImage = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB);
        mImage.getGraphics().drawImage(image, 0, 0, rect.width, rect.height, rect.x, rect.y, rect.x + rect.width, rect.y + rect.height, null);
        mOriginalPosition = new Point(rect.x, rect.y);

        // 添加到父窗口上
        parent.add(this);

        // 添加鼠标事件监听者
        super.addMouseListener(this);
    }

    /**
     * 画边框。
     * @param drawer    绘图者
     */
    private void drawEdge(Graphics drawer) {
        final int size = super.getWidth();
        
        drawer.setColor(Const.COLOR_LEFT_TOP_1);
        drawer.drawLine(0, 0, size - 1, 0);
        drawer.drawLine(0, 0, 0, size - 1);
        
        drawer.setColor(Const.COLOR_LEFT_TOP_2);
        drawer.drawLine(1, 1, size - 2, 1);
        drawer.drawLine(1, 1, 1, size - 2);
        
        drawer.setColor(Const.COLOR_RIGHT_BOTTOM_1);
        drawer.drawLine(size - 1, 0, size - 1, size - 1);
        drawer.drawLine(0, size - 1, size - 1, size - 1);
        
        drawer.setColor(Const.COLOR_RIGHT_BOTTOM_2);
        drawer.drawLine(size - 2, 1, size - 2, size - 2);
        drawer.drawLine(1, size - 2, size - 2, size - 2);
    }
    
    /**
     * 判断自身是否在初始位置。
     * @return 在初始位置则返回 true，否则返回 false
     */
    public boolean isOnOriginalPosition() {
        return super.getX() == mOriginalPosition.x && super.getY() == mOriginalPosition.y;
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
    }

    /**
     * 鼠标键抬起事件的响应方法。
     * @param e 事件相关信息
     */    
    @Override
    public void mouseReleased(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
            mCallback.movePicture(this);
        }
    }

    /**
     * 重绘事件的响应方法。
     * @param drawer    绘图者
     */
    @Override
    public void paintComponent(Graphics drawer) {
        drawer.drawImage(mImage, 0, 0, null);
        if (mHasBorder) {
            this.drawEdge(drawer);
        }
    }

    /**
     * 设置边框。
     * @param hasBorder 有边框则为 true，否则为 false
     */
    public void setBorder(boolean hasBorder) {
        if (mHasBorder == hasBorder) {
            return;
        }
        mHasBorder = hasBorder;
        super.repaint();
    }
}
